package com.popcorn.login.controller;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.popcorn.action.Action;
import com.popcorn.action.ActionForward;
import com.popcorn.login.dao.LoginDAO;
import com.popcorn.login.model.LoginVO;

public class PasswordSearchAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//파라미터로 전송된 아이디와 비밀번호 얻어오기
				String id=request.getParameter("id");
				String name=request.getParameter("name");
				ActionForward forward= new ActionForward();
				
				LoginVO vo=new LoginVO();
				vo.setId(id);
				vo.setName(name);
				System.out.println(id+"/"+name);
				//해당정보가 DB에 있는지 검사
				LoginDAO dao= new LoginDAO();
				String email = dao.passwordSearch(id,name);
				if(email != null){ //아이디 비밀번호가 모두 맞는경우
					String pw = dao.randomPassword(email);
					System.out.println(pw);
					sendEmail(email,pw);
					String msg="이메일 전송 성공";
					System.out.println(msg);
					forward.setRedirect(false);
			   		forward.setPath("../main/main.jsp");
			   		return forward;
				}else{//아이디 또는 비밀번호가 틀린경우
					String msg="아이디 또는 비밀번호가 틀려요!";
					System.out.println(msg);
					forward.setRedirect(false);
			   		forward.setPath("../login/login.jsp");
			   		return forward;
				}
	}

	private void sendEmail(String email, String pw) throws MessagingException{
        // 메일 관련 정보
        String host = "smtp.naver.com";
        final String username = "hv708";
        final String password = "dnjscjf12";
        int port=465;
         
        // 메일 내용
        String recipient = email; // 받는 사람 이메일
        String subject = "팝콘 홈페이지 비밀번호 입니다.";
        String body = "변경 된 비밀번호 : "+pw;
         
        Properties props = System.getProperties();
         
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.trust", host);

        Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            String un=username;
            String pw=password;
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(un, pw);
            }
        });
        session.setDebug(true); //for debug

        Message mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("hv708@naver.com"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
        mimeMessage.setSubject(subject);
        mimeMessage.setText(body);
        Transport.send(mimeMessage);
    }
}